// 引入express模块
const express = require("express")
// 定义路由级中间件
const router = express.Router()
// 引入数据模型模块
const ReplyComment = require("../model/replycomment")

// 获取所有站长回复
router.get("/replyComment", (req, res) => {
  ReplyComment.find({})
    .sort({ update_at: -1 })
    .then(replyComments => {
      res.json(replyComments)
    })
    .catch(err => {
      console.log('查询报错')
      console.log(err)
      res.json(err)
    })
})

// 获取当前文章的所有站长回复
router.get("/replyComment/:id", (req, res) => {
  ReplyComment.find({ articleId: req.params.id })
    .sort({ update_at: -1 })
    .then(replyComments => {
      res.json(replyComments)
    })
    .catch(err => {
      console.log('查询报错')
      console.log(err)
      res.json(err)
    })
})

// 发表站长评论
router.post("/replyComment", (req, res) => {
  ReplyComment.create(req.body, (err, replyComments) => {
    if (err) {
      res.json(err)
    } else {
      res.json(replyComments)
    }
  })
})

// 删除一条评论
router.delete("/replyCommentDelete/:id", (req, res) => {
  ReplyComment.findByIdAndRemove({
    _id: req.params.id
  })
    .then(replyComments => res.json(replyComments))
    .catch(err => res.json(err))
})
module.exports = router
